/*
 * CS_CREATE_SLAVE.cpp
 *
 *  Created on: 2013. 5. 21.
 *      Author: the2
 */

#include "../../User.h"
#include "../../NLogic/Function.h"
#include "../../NLogic/Static.h"
#include "../../NResource/Static.h"

#include "CS_CREATE_SLAVE.h"

namespace ProjectZ { namespace NCommand { namespace NBattle {

void CS_CREATE_SLAVE::OnExecute(User * pUser, ::Network::Packet * pPacket)
{
/*	//소환몹 GID할당 요청 C->S
	CS_CREATE_SLAVE		0x0306
	{
		u32 u32gid;
		u16 u16serial;
		u16 u16pos_x;
		u16 u16pos_y;
		u16 u16degree;
		u8  u8sender_type;   	// (0 : 주인공 1: 몬스터)
		u8  u8slave_type;		// (0 : 주인공소환 1: 몬스터소환)
		s16 s16life_frame;		// 유지 시간
		s16 s16set_dir;			// -1 일반몹 : 0보다 크면 보조몹
		s16 s16level;
		s8 s8pet_type;			/// 전투펫 타입 -1 없음, 0 : 공격형, 1: 방어형, 2: 보조형
		s8 s8pet_element_type;	/// 원소타입 -1 없음, 0: 신성, 1:냉기, 2: 암흑
		s8 s8qulity;			/// 전투펫 등급
	}

	//소환몹 GID할당 요청 C->S
	/// 서버는 s8pet_type이 0 이상이면 u32hp 타입별 가중치 세팅해서 내려줘야함
	SC_CREATE_SLAVE		0x0307
	{
		u32 u32set_gid;
		u32 u32gid;
		u16 u16serial;
		u32 u32hp;
		u16 u16pos_x;
		u16 u16pos_y;
		u16 u16degree;
		u8  u8sender_type;   	// (0 : 주인공 1: 몬스터)
		u8  u8slave_type;	// (0 : 주인공소환 1: 몬스터소환)
		s16 s16life_frame;	// 유지 시간
		s16 s16set_dir;		// -1 일반몹 : 0보다 크면 보조몹
		s8 s8pet_type;			/// 전투펫 타입 -1 없음, 0 : 공격형, 1: 방어형, 2: 보조형
		s8 s8pet_element_type;	/// 원소타입 -1 없음, 0: 화염, 1:냉기, 2: 신성, 3: 암흑
		s8 s8qulity;			/// 전투펫 등급 몇성,..
	}*/

	NLogic::Space * pSpace = pUser->GetSpace();
	if (NULL == pSpace)
	{
		DEBUGLOG("CS_CREATE_SLAVE NULL == pSpace");
		return;
	}

//	if (pSpace->GetData()._spaceType != NLogic::Space::SpaceType::SINGLE_DUNGEION &&
//			pSpace->GetData()._spaceType != NLogic::Space::SpaceType::PARTY_DUNGEION )
//	{
//		DEBUGLOG("CS_CREATE_SLAVE space is not single or party dungeion");
//		return;
//	}

	PREPARE_PACKET;

	int gid = 0;
	uint16_t serial = 0;	// TID
	//int hp = 0;
	uint16_t x = 0, y = 0, degree = 0;
	uint8_t sender_type = 0;		// (0 : 주인공 1: 몬스터)
	uint8_t slave_type = 0;		// (0 : 주인공소환 1: 몬스터소환)
	int16_t life_frame = 0, set_dir, level= 0;
	int8_t pet_type = 0;
	int8_t pet_element_type = 0;
	int8_t pet_quality = 0;
	int8_t pet_stamina_step = 0;

	* pPacket >> gid >> serial >> x >> y >> degree >> sender_type >> slave_type >> life_frame >> set_dir >> level >> pet_type >> pet_element_type >> pet_stamina_step >> pet_quality;
	//pSlave->SetGID(serial);

	if (slave_type != 0 && slave_type != 1)
	{
		PROCESSLOG("CS_CREATE_SLAVE SLAVE_TYPE IS ERROR : %d", slave_type);
		return;
	}

	int slaveGid = pSpace->GetSlaveGID();

	int dungeon_mode = pSpace->GetData()._difficulty;
	int player_count = pSpace->GetData()._currentUser;
	int table_id = pSpace->GetData()._table_id;

	int monster_hp = 0;

	// 전투펫 소환인 경우 타입에 따라 HP 가중치 부여
	if (0 <= pet_type && pet_type <= 2)
	{
		monster_hp = NLogic::NFunction::GetMonsterHP(pUser, level, dungeon_mode, table_id, player_count, false, false, true);
		NResource::Static::InstancePtr()->GetBattlepetGachya()->AddBattlePetHP(pUser, pet_type, monster_hp);
		monster_hp = (monster_hp * (100 + pet_quality * 10)) / 100;
		TESTLOG("%s : hp=%d, quality=%d, type=%d", __FUNCTION__, monster_hp, pet_quality, pet_type);
	}
	else
	{
		if (pSpace->GetData()._spaceType == NLogic::Space::SpaceType::PVP_PRACTICE
				|| pSpace->GetData()._spaceType == NLogic::Space::SpaceType::PVP_USER
				|| pSpace->GetData()._spaceType == NLogic::Space::SpaceType::NEW_PVP_ROOM
				|| pSpace->GetData()._spaceType == NLogic::Space::SpaceType::BATTLEROYAL)
		{
			monster_hp = NLogic::NFunction::GetMonsterHP(pUser, level, dungeon_mode, table_id, player_count, false, false, true);
		}
		else
		{
			monster_hp = NLogic::NFunction::GetMonsterHP(pUser, level, dungeon_mode, table_id, player_count, false, false);
		}

		monster_hp = monster_hp * ((15 + level / 4) / 100.0);
	}

	//
	::Network::Packet * pNoti = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pNoti << slaveGid << gid << serial << monster_hp << x << y << degree <<  sender_type << slave_type << life_frame << set_dir << level << pet_type << pet_element_type << pet_stamina_step << pet_quality;
	pNoti->MakePacket(CMD_SC_CREATE_SLAVE);
	pSpace->SendAll(pNoti);

	NLogic::Monster::Data clsData;
	clsData.gid = slaveGid;
	clsData.hp = monster_hp;
	clsData.max_hp = monster_hp;
	clsData.map_index = -1;
	clsData.x = x;
	clsData.y = y;
	clsData.tid = table_id;
	clsData.level = 0;
	clsData.islegend = false;
	clsData.degree = degree;
	clsData.isslave = 0;

	NLogic::Monster * pMonster = NLogic::Static::Instance().NEW_MONSTER();
	if (slave_type == 0)
	{
		pMonster->Open(clsData, NLogic::Monster::MonsterType::PLAYER_SLAVE);
		pSpace->AddMonster(pMonster);
	}
	else if (slave_type == 1)
	{
		pMonster->Open(clsData, NLogic::Monster::MonsterType::MOB_SLAVE);
		pSpace->AddMonster(pMonster);
	}
	else
	{
		DEBUGLOG("CS_CREATE_SLAVE SLAVE_TYPE IS ERROR : %d", slave_type);
		return;
	}

	//pSpace->AddMonster(pMonster);
}

} /*NBattle*/ } /*NCommand*/ } /*ProjectZ*/
